package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.app.AppConstant;
import com.shqcjd.yunweiyanoperator.app.MyApp;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.CollectionBean;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.AppManager;
import com.shqcjd.yunweiyanoperator.util.PrefUtils;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 修改密码
 */
public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_original_pwd)
    EditText etOriginalPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.tv_commit)
    TextView tvCommit;

    private Context mContext;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_change_password;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle.setText("修改登录密码");
    }

    @OnClick({R.id.btn_finish, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.tv_commit:
                String originalPwd = etOriginalPwd.getText().toString().trim();
                String newPwd = etNewPwd.getText().toString().trim();
                String confirmPwd = etConfirmPwd.getText().toString().trim();
                if(!originalPwd.equals("")&&!newPwd.equals("")&&!confirmPwd.equals("")){
                    if(newPwd.equals(confirmPwd)){
                        if(originalPwd.equals(PrefUtils.getString(mContext, PrefUtils.USER_PWD, ""))){
                            if(confirmPwd.length()>=6){
                                updateHttp(confirmPwd);
                            }else{
                                ToastUtils.showShort(mContext, "密码必须大于等于6位");
                            }
                        }else{
                            etOriginalPwd.setText("");
                            ToastUtils.showShort(mContext, "原密码错误，请重新输入");
                        }
                    }else{
                        ToastUtils.showShort(mContext, "两次密码不一致");
                    }
                }else{
                    ToastUtils.showShort(mContext, "请确认所有信息填写完毕");
                }
                break;
        }
    }

    private void updateHttp(String pwd) {
        GSYVideoManager.releaseAllVideos();
        Map<String, String> map = new HashMap<>();
        map.put("password", pwd);
        ApiService.updatePassword(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("修改中");
                    }

                    @Override
                    public void _onNext(ResponseData responseData) {
                        if (responseData.retCode == 10000) {
                            ToastUtils.showShort(mContext, "修改成功");
                            signOut();
                        } else {
                            ToastUtils.showShort(mContext, responseData.retMsg);
                        }
                        hideWaitingDialog();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    /**
     * 退出登录
     */
    private void signOut() {
        MyApp.the().getmAgoraAPI().logout();
        PrefUtils.setBoolean(this, PrefUtils.FIRST_LOGIN, false);
        PrefUtils.setInt(this, PrefUtils.USER_ID, -1);
        PrefUtils.setString(this, PrefUtils.USER_CODE, "");
        PrefUtils.setString(this, PrefUtils.USER_NAME, "");
        PrefUtils.setString(this, PrefUtils.PHONE_NUMBER, "");
        PrefUtils.setString(this, PrefUtils.USER_ICON, "");
        PrefUtils.setString(this, AppConstant.TOKEN_KEY, "");
        PrefUtils.setInt(this, PrefUtils.USER_TYPE, -1);
        PrefUtils.setString(this, PrefUtils.USER_ACCOUNT, "");
        PrefUtils.setString(this, PrefUtils.USER_PWD, "");
        PrefUtils.setString(this, PrefUtils.TOKEN, "");
        PrefUtils.setInt(this, PrefUtils.USER_STATE, -1);
        PrefUtils.setString(this, PrefUtils.BUSINESSId, "");
        PrefUtils.setString(this, PrefUtils.APPCODE, "");
        PrefUtils.setInt(mContext, PrefUtils.ORGANIZATION_TYPE, -1);
        PrefUtils.setString(mContext, PrefUtils.BUSINESSNAME, "");
        AppManager.getAppManager().finishAllActivity();
        Intent it = new Intent(this, LoginActivity.class);
        startActivity(it);
    }
}

